﻿using System.Drawing.Drawing2D;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System;
using System.Web;
using System.Drawing.Text;
using System.Web.UI;
using Marbles.Core.Web.Controls;

namespace Marbles.Core.Web.Images
{
    public class HeaderTextImageHandler : HttpHandlerBase, IHttpHandler
    {

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Params["s"] != null)
            {
                RenderText(context);
            }

            if (context.Request.Params["l"] != null)
            {
                RenderFontList(context);
            }
        }

        private void RenderFontList(HttpContext context)
        {
            float width = 0;
            float height = 0;
            InstalledFontCollection fontcoll = new InstalledFontCollection();

            Bitmap bmp = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(bmp);
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            for (int i = 0; i < fontcoll.Families.Length; i++)
            {
                try
                {
                    Font fontface = new Font(fontcoll.Families[i], 10);
                    if (fontface.Name != string.Empty)
                    {
                        SizeF textsize = g.MeasureString(fontface.Name, fontface);
                        if ((textsize.Width * 2) > width) { width = (textsize.Width * 2); }
                        height += textsize.Height + 2;
                    }
                }
                catch { }
            }

            g.Dispose();
            bmp.Dispose();
            bmp = new Bitmap((int)width, (int)height);
            g = Graphics.FromImage(bmp);
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            height = 0;
            Font fontface1 = new Font("Arial", 10);

            for (int i = 0; i < fontcoll.Families.Length; i++)
            {
                try
                {
                    Font fontface = new Font(fontcoll.Families[i], 10);

                    if (fontface.Name != string.Empty)
                    {
                        SizeF textsize = g.MeasureString(fontface.Name, fontface);
                        height += textsize.Height + 2;
                        g.DrawString(fontface.Name, fontface, Brushes.Black, 0, height);
                        g.DrawString(fontface.Name, fontface1, Brushes.Black, 0 + textsize.Width + 10, height);
                    }
                }
                catch { }
            }

            ImageHandler.SaveToOutputStream(context, bmp, ImageFormat.Png);
            g.Dispose();
            bmp.Dispose();
        }

        private void RenderText(HttpContext context)
        {
            LosFormatter fmnt = new LosFormatter();
            HeaderTextSettings settings = fmnt.Deserialize(new StringReader(context.Request.Params["s"])) as HeaderTextSettings;

            string[] font = settings.Font.Split(',');
            Color bc = Color.White;
            Color fc = Color.Black;

            if (!string.IsNullOrEmpty(settings.Fc))
            {
                fc = ColorTranslator.FromHtml(settings.Fc);
            }

            if (!string.IsNullOrEmpty(settings.Bc))
            {
                bc = ColorTranslator.FromHtml(settings.Bc);
            }

            FontStyle fontstyle = FontStyle.Regular;
            if (Convert.ToBoolean(font[0])) { fontstyle = fontstyle & FontStyle.Italic; }
            if (Convert.ToBoolean(font[1])) { fontstyle = fontstyle & FontStyle.Bold; }
            if (Convert.ToBoolean(font[2])) { fontstyle = fontstyle & FontStyle.Strikeout; }
            if (Convert.ToBoolean(font[3])) { fontstyle = fontstyle & FontStyle.Underline; }

            Font fontface = new Font(font[5], Convert.ToSingle(font[4]), fontstyle);


            Bitmap bmp = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(bmp);
            SizeF textsize = g.MeasureString(settings.Text, fontface);
            bmp.Dispose();
            g.Dispose();
            bmp = new Bitmap(((int)textsize.Width), ((int)textsize.Height), PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(bmp);
            g.Clear(bc);
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.DrawString(settings.Text, fontface, new SolidBrush(fc), new PointF(0, 0));

            ImageHandler.SaveToOutputStream(context, bmp, ImageFormat.Png);
            g.Dispose();
            bmp.Dispose();
        }

        public override void Register()
        {
            RegisterHttpHandler("HeaderText.axd", "Marbles.Core.Web.Images.HeaderTextImageHandler", "GET,HEAD");
        }
    }
}
